package casa.depesos.app.com.adapter;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import java.util.HashMap;
import java.util.List;

import casa.depesos.app.com.App;
import casa.depesos.app.com.R;
import casa.depesos.app.com.activity.OrderDetailActivity;
import casa.depesos.app.com.databinding.OrderItemBinding;
import casa.depesos.app.com.http.data.Order;
import casa.depesos.app.com.utils.Utils;

public class OrderListAdapter extends RecyclerView.Adapter {

    List<Order> list;
    HashMap<String, String> map = new HashMap<>();

    public OrderListAdapter() {
        String[] title = App.getApp().getResources().getStringArray(R.array.orders);
        String[] status = App.getApp().getResources().getStringArray(R.array.orders_status);
        for (int i = 0; i < status.length; i++) {
            map.put(status[i], title[i]);
        }
    }

    public void setList(List list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        OrderItemBinding binding = OrderItemBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        return new Holder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof Holder) {
            ((Holder) holder).bind(position);
        }
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    class Holder extends RecyclerView.ViewHolder {
        OrderItemBinding binding;

        Holder(OrderItemBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        void bind(int p) {
            Order item = list.get(p);
            binding.amount.setText(Utils.formatNoFloat(item.amount));
            if (map.containsKey(item.status)) {
                binding.status.setText(map.get(item.status));
            } else {
                binding.status.setText("");
            }
            binding.date.setText(Utils.getDay3(item.time));
            try {
                binding.day.setText((Float.valueOf(item.day).intValue() + 1) + "days");
            } catch (Throwable e) {
                binding.day.setText(item.day);
            }
            if ("true".equalsIgnoreCase(item.isOverDue)) {
                binding.overDay.setText(String.format(App.getApp().getString(R.string.overday), item.overDueDay));
            }
            switch (item.status) {
                case "10":
                    binding.status.setBackgroundResource(R.drawable.order_10);
                    break;
                case "70":
                    binding.status.setBackgroundResource(R.drawable.order_red);
                    break;
                case "20":
                    binding.status.setBackgroundResource(R.drawable.order_20);
                    break;
                case "30":
                    binding.status.setBackgroundResource(R.drawable.order_30);
                    break;
                case "40":
                    if ("true".equalsIgnoreCase(item.isOverDue)) {
                        binding.status.setBackgroundResource(R.drawable.order_10);
                    } else {
                        binding.status.setBackgroundResource(R.drawable.order_gray);
                    }
                    break;
                case "60":
                    binding.status.setBackgroundResource(R.drawable.order_60);
                    break;
            }
            itemView.setOnClickListener(v -> {
                Intent i = new Intent(App.getApp(), OrderDetailActivity.class);
                i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                i.putExtra("order", list.get(getBindingAdapterPosition()));
                App.getApp().startActivity(i);
            });
        }
    }
}
